package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.InviteRecordDto;
import io.geekidea.boot.shop.entity.InviteRecord;
import io.geekidea.boot.shop.mapper.InviteRecordMapper;
import io.geekidea.boot.shop.query.app.AppInviteRecordQuery;
import io.geekidea.boot.shop.query.web.InviteRecordQuery;
import io.geekidea.boot.shop.service.base.api.InviteRecordService;
import io.geekidea.boot.shop.vo.app.AppInviteRecordVo;
import io.geekidea.boot.shop.vo.web.InviteRecordVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class InviteRecordServiceImpl extends ServiceImpl<InviteRecordMapper, InviteRecord> implements InviteRecordService {

    @Autowired
    private InviteRecordMapper inviteRecordMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addInviteRecord(InviteRecordDto dto) {
        InviteRecord inviteRecord = new InviteRecord();
        BeanUtils.copyProperties(dto, inviteRecord);
        return save(inviteRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateInviteRecord(InviteRecordDto dto) {
        Integer id = dto.getId();
        InviteRecord inviteRecord = getById(id);
        if (inviteRecord == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, inviteRecord);
        return updateById(inviteRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteInviteRecord(Integer id) {
        return removeById(id);
    }

    @Override
    public InviteRecordVo getInviteRecordById(Integer id) {
        return inviteRecordMapper.getInviteRecordById(id);
    }

    @Override
    public Paging<InviteRecordVo> getInviteRecordPage(InviteRecordQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<InviteRecordVo> list = inviteRecordMapper.getInviteRecordPage(query);
        Paging<InviteRecordVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppInviteRecordVo getAppInviteRecordById(Integer id) {
        return inviteRecordMapper.getAppInviteRecordById(id);
    }

    @Override
    public Paging<AppInviteRecordVo> getAppInviteRecordPage(AppInviteRecordQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppInviteRecordVo> list = inviteRecordMapper.getAppInviteRecordPage(query);
        Paging<AppInviteRecordVo> paging = new Paging<>(list);
        return paging;
    }

}
